﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Data;
using System.Text;

namespace PeisongWeb
{
    public partial class buyer_favoriteWeekEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                week = Convert.ToInt32(Request.QueryString["week"]);
                iframeSelectedList.Attributes["src"] = "iframe_buyer_favourite.html?week=" + week;

                categoryRootBind();
                categorySubBind();
                rptBind();
            }
        }

        protected int week
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["week"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["week"] = value; }
        }

        void categoryRootBind()
        {
            int companyId = logic.customer.getCompanyId();
            DataTable dt = logic.buyerRange.rootCategorylistWithCompanyRange(companyId);
            ddl_CategoryRoot.DataSource = dt;
            ddl_CategoryRoot.DataBind();
        }

        void categorySubBind()
        {
            int companyId = logic.customer.getCompanyId();
            DataTable dt = logic.buyerRange.subCategory_listWithCompanyRange(companyId, Convert.ToInt32(ddl_CategoryRoot.SelectedValue));
            ddl_CategorySub.DataSource = dt;
            ddl_CategorySub.DataBind();
        }

        void rptBind()
        {
            StringBuilder where = new StringBuilder();

            if (ddl_CategorySub.SelectedValue != "")
            {
                where.Append(" isSale=1 and categoryId=" + ddl_CategorySub.SelectedValue);

                //where.Append(" and (select COUNT(*) from vw_SellerRange_list WHERE companyId="+ logic.customer.getCompanyId()+" AND vw_Product_list.productId=vw_SellerRange_list.productId )=0");


                DataSet ds = logic.product.select(where.ToString(), "productId", 100, 1);
                rpt_list.DataSource = ds.Tables[1];
                rpt_list.DataBind();
            }
            else
            {
                rpt_list.DataBind();
            }
        }

        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            categorySubBind();
        }

        protected void ddl_CategorySub_SelectedIndexChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        void addProduct(int productId)
        {
            CompanyWeekEntity cw = new CompanyWeekEntity();
            int companyId = logic.customer.getCompanyId();
            cw = logic.companyWeek.getByWeek(week, companyId);
            if (cw.CsId != 0)
            {
                string cw_pros = cw.Products;
                string[] splitstr = { "," };
                string[] pros = cw_pros.Split(splitstr, StringSplitOptions.RemoveEmptyEntries);
                //bool hasProduct=false;

                bool hasProduct = pros.Contains<string>(productId.ToString());

                //for (int i = 0; i < pros.Length; i++)
                //{
                //    if (pros[i] == productId.ToString())
                //    {
                //        hasProduct = true;
                //        break;
                //    }
                //}
                if (!hasProduct)
                {
                    cw.Products = productId.ToString() + "," + cw_pros;
                    logic.companyWeek.update(cw);
                }


            }
            else
            {
                cw.CustomerId = logic.customer.userId;
                cw.CompanyId = companyId;
                cw.Weekday = week;
                cw.Products = productId.ToString() + ",";
                logic.companyWeek.insert(cw);

            }
        }

        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Sel")
            {
                int productId = Convert.ToInt32(e.CommandArgument);
                addProduct(productId);
                rptBind();
                iframeSelectedList.Attributes["src"] = "iframe_buyer_favourite.html?week=" + week;
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "alert('保存成功！')", true);
            }
        }
    }
}
